package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.BussGoodsTypeDao;
import com.baichang.taoshunda.dao.GoodsSpecDao;
import com.baichang.taoshunda.dao.SpecValueDao;
import com.baichang.taoshunda.entity.BussGoodsType;
import com.baichang.taoshunda.entity.GoodsSpec;
import com.baichang.taoshunda.entity.SpecValue;
import com.baichang.taoshunda.service.BussGoodsTypeService;
import com.baichang.taoshunda.service.GoodsService;
import com.baichang.taoshunda.service.GoodsSpecService;
import com.baichang.taoshunda.service.GoodsTypeService;
import com.baichang.taoshunda.utils.ConvertTypeUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2017/8/22 0022.
 */
@Service
public class BussGoodsTypeServiceImpl implements BussGoodsTypeService {
    @Autowired
    private BussGoodsTypeDao bussGoodsTypeDao;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsSpecDao goodsSpecDao;
    @Autowired
    private SpecValueDao specValueDao;

    @Autowired
    private GoodsSpecService goodsSpecService;
    @Autowired
    private GoodsTypeService goodsTypeService;

    /**
     * 查询商家下的商品分类
     * 接口
     * 武圣路
     *
     * @param bussId 商家编号
     * @param userId
     * @return
     */
    @Override
    public List<Map<String, Object>> getGoodsTypeByBussId(int bussId, Integer userId) {
        List<Map<String, Object>> returnMapList = new ArrayList<Map<String, Object>>();
        //获取商家商品所有的分类
        List<Map<String, Object>> typeList = goodsTypeService.findCompanyGoodsType(bussId);
        if (typeList != null && typeList.size() > 0) {
            for (Map<String, Object> map : typeList) {
                Integer typeId = ConvertTypeUtils.obj2Integer(map.get("id"));
                //查询商品列表
                List<Map<String, Object>> goodsList = goodsService.findGoodsListByTypeIdAndShopCar(typeId, userId);
                Map<String, Object> newMap = map;
                newMap.put("goodsList", goodsList);
                returnMapList.add(newMap);
            }
        }

        return returnMapList;
}

    /**
     * 添加商品类别
     *
     * @param bussId 商家编号
     * @param name   类别名称
     * @return
     */
    @Override
    public boolean addGoodsType(Integer bussId, String name, String specData) {
        BussGoodsType bussGoodsType = new BussGoodsType();
        bussGoodsType.setBusId(bussId);
        bussGoodsType.setCreated(new Date());
        bussGoodsType.setRank(0);
        bussGoodsType.setState(1);
        bussGoodsType.setTypeName(name);
        Long bussGoodsTypeId = bussGoodsTypeDao.save(bussGoodsType);
//        JSONArray allData = JSONArray.fromObject(specData);
//        if (allData != null && allData.size() > 0) {
//            for (int i = 0; i < allData.size(); i++) {
//                JSONObject spec = (JSONObject) allData.get(i);
//                String specName = spec.get("name").toString();
//                String specValues = spec.get("values").toString();
//                goodsSpecService.addSpec(bussId, bussGoodsTypeId.intValue(), specName, specValues);
//            }
//        }
        JSONArray allData = JSONArray.fromObject(specData);
        if (allData != null && allData.size() > 0) {
            for (int i = 0; i < allData.size(); i++) {
                JSONObject spec = (JSONObject) allData.get(i);
                String value = spec.get("value").toString();
                SpecValue specValue = new SpecValue();
                specValue.setSpecId(0);
                specValue.setTypeId(ConvertTypeUtils.obj2Integer(bussGoodsTypeId));
                specValue.setSpecValues(value);
                specValueDao.save(specValue);
            }
        }
        return true;
    }


    /**
     * 修改商品类别
     *
     * @param id   类别编号
     * @param name 类别名称
     * @return
     */
    @Override
    public boolean updateGoodsType(Integer id, String name, String specData) {
        bussGoodsTypeDao.updateGoodsType(id, name);
        specValueDao.deleteByTypeId(id);
        JSONArray specValuesArray = JSONArray.fromObject(specData);
        if (specValuesArray != null && specValuesArray.size() > 0) {
            for (int i = 0; i < specValuesArray.size(); i++) {
                JSONObject jsonObject = (JSONObject) specValuesArray.get(i);
                String value = jsonObject.get("value").toString();
                SpecValue specValue = new SpecValue();
                specValue.setSpecId(0);
                specValue.setTypeId(id);
                specValue.setSpecValues(value);
                specValueDao.save(specValue);
            }
        }
        return true;
    }

    /**
     * 删除商品类别
     *
     * @param id 编号
     * @return
     */
    @Override
    public boolean deleteGoodsType(Integer id) throws ServiceException {
        int goodsCount = goodsService.getGoodsCountByGoodsTypeId(id);
        if (goodsCount == 0) {
            return bussGoodsTypeDao.updateGoodsTypeState(id, 0) > 0;
        }
        throw new ServiceException("请优先删除该分类下的商品");
    }

}
